import request from '../../utils/request'
import moment from 'moment'
var appInstance = getApp()
var i = 0
var k = 99999
var first = 1
// pages/songDetail/songDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isplay: false,
        song: {},
        recommendList:[],
        indexk:'',
        durationtime:'00:00',
        currenttime:'00:00'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const song = decodeURIComponent(options.song)
        this.setData({
            song: JSON.parse(song),
        })
        // let durationtime = moment(this.data.song.dt).format('mm:ss');
        // this.setData({
        //     durationtime
        // })
        // console.log(JSON.stringify(song))
        if(appInstance.globalData.isMusicPlay && appInstance.globalData.musicid === this.data.song.id){
            this.setData({
                isplay: true
            })
        }
        this.backgroundAudioManager = wx.getBackgroundAudioManager();
        this.backgroundAudioManager.onPlay(() => {
            this.changePlayState(true);
            appInstance.globalData.musicid = this.data.song.id;
        });
        this.backgroundAudioManager.onPause(() => {
            this.changePlayState(false);
        });
        this.backgroundAudioManager.onStop(() => {
            this.changePlayState(false);
        });
        this.backgroundAudioManager.onEnded(() => {
            this.next_swithmusic()
        })

        this.backgroundAudioManager.onTimeUpdate(() => {
            // console.log('总长；', this.backgroundAudioManager.duration);
            // console.log('实时；', this.backgroundAudioManager.currentTime);
            let currenttime = moment(this.backgroundAudioManager.currentTime*1000).format('mm:ss');
            this.setData({
                currenttime
            })
        })
    },
    changePlayState(isplay){
        this.setData({
            isplay
        })
        appInstance.globalData.isMusicPlay = isplay
    },
    musicplay(){
        let isplay = !this.data.isplay
        this.setData({
            isplay
        })
        console.log(first)
        this.musicControl(isplay, this.data.song.id)
        console.log(this.data.song.id)
        console.log(this.data.song.al.name)
    },
    async musicControl(isplay, id){
        let backgroundAudioManager = wx.getBackgroundAudioManager();
        if(isplay){
            let musicLinkData = await request('/song/url', {id: id})
            let musicLink = musicLinkData.data[0].url;
            // backgroundAudioManager.title = this.data.recommendList[k].al.name?this.data.recommendList[k].al.name:this.data.song.al.name 
            if(first === 1){
                backgroundAudioManager.title = this.data.song.al.name
            }else if(first === 0){
                backgroundAudioManager.title = this.data.recommendList[k].al.name
            }
            first = 1
            // backgroundAudioManager.title = this.data.song.al.name
            backgroundAudioManager.src = musicLink
        }else {
            backgroundAudioManager.pause();
        }
    },
    // swithmusic(event){
    //     let idx = event.currentTarget.id
    //      while(i<=this.data.recommendList.length && this.data.song.id !== this.data.recommendList[i].id){
    //         i++;
    //      }
    //      first = 0
    //      if(idx === 'pre'){
    //         if(i === 0){
    //             wx.showToast({
    //               title: '这是第一首',
    //               icon: 'error'
    //             })
    //             return;
    //         }
    //         k = i-1
    //         this.setData({
    //             indexk: k
    //         })
    //          this.data.song.id = this.data.recommendList[k].id
    //         this.musicControl(true, this.data.recommendList[k].id)
    //      }else if(idx === 'next'){
    //         if(i === this.data.recommendList.length - 1){
    //             wx.showToast({
    //               title: '这是最后一首',
    //               icon: 'error'
    //             })
    //             return;
    //         }
    //         k = i+1
    //         this.setData({
    //             indexk: k
    //         })
    //          this.data.song.id = this.data.recommendList[k].id
    //         this.musicControl(true, this.data.recommendList[k].id)
    //      }
    //      i = 0
    // },
    pre_swithmusic(){
        while(i<=this.data.recommendList.length && this.data.song.id !== this.data.recommendList[i].id){
                    i++;
                 }
                 first = 0
                    if(i === 0){
                        wx.showToast({
                          title: '这是第一首',
                          icon: 'error'
                        })
                        return;
                    }
                    k = i-1
                    this.setData({
                        indexk: k
                    })
                     this.data.song.id = this.data.recommendList[k].id
                    this.musicControl(true, this.data.recommendList[k].id)
                    i = 0
    },
    next_swithmusic(){
        while(i<=this.data.recommendList.length && this.data.song.id !== this.data.recommendList[i].id){
            i++;
         }
         first = 0
                    if(i === this.data.recommendList.length - 1){
                        wx.showToast({
                          title: '这是最后一首',
                          icon: 'error'
                        })
                        return;
                    }
                    k = i+1
                    this.setData({
                        indexk: k
                    })
                     this.data.song.id = this.data.recommendList[k].id
                    this.musicControl(true, this.data.recommendList[k].id)
                 
                 i = 0
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let pages = getCurrentPages();//当前页面栈
        let prevPage = pages[pages.length - 2];//上一页面
         this.setData({
             recommendList:prevPage.data.recommendList
         })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})